import scrapy
from scrapy import Selector, Request

from bys.items import LnjydwItem


class LnjydwSpider(scrapy.Spider):
    """爬取往届生去向"""
    name = 'lnjydw'
    allowed_domains = ['cqu.cqbys.com']
    start_urls = ['https://cqu.cqbys.com/affair/lnjydw']

    def parse(self, response, **kwargs):
        sel = Selector(response)
        grades = [23]  # sel.css('div.schBox > form > div:nth-child(1) > select > option::attr(value)').extract()
        educations = sel.css('div.schBox > form > div:nth-child(2) > select > option::attr(value)').extract()
        schools = sel.css('div.schBox > form > div:nth-child(3) > select > option::attr(value)').extract()
        destinations = sel.css('div.schBox > form > div:nth-child(5) > select > option::attr(value)').extract()
        for school in filter(None, schools):
            for grade in filter(None, grades):
                for education in filter(None, educations):
                    for destination in filter(None, destinations):
                        yield Request(
                            url=f'https://cqu.cqbys.com/affair/lnjydw/?grade={grade}&education={education}&school={school}&major=&University1950Lnjydw%5Bjylb%5D={destination}&submit=1',
                            callback=self.parse_major,
                            cb_kwargs={
                                'grade': grade,
                                'education': education,
                                'school': school,
                                'destination': destination
                            }
                        )

    def parse_major(self, response, **kwargs):
        sel = Selector(response)
        cb_kwargs = {
            'grade': sel.css('div.schBox > form > div:nth-child(1) > select > option[selected]::text').extract_first(),
            'education': sel.css('div.schBox > form > div:nth-child(2) > select > option[selected]::text').extract_first(),
            'school': sel.css('div.schBox > form > div:nth-child(3) > select > option[selected]::text').extract_first(),
            'destination': sel.css('div.schBox > form > div:nth-child(5) > select > option[selected]::text').extract_first()
        }
        majors = sel.css('div.schBox > form > div:nth-child(4) > select > option')
        for major in majors:
            value = major.css('option::attr(value)').extract_first()
            if len(majors) > 1 and value == '':
                continue
            cb_kwargs['major'] = major.css('option::text').extract_first()
            yield Request(
                url=f'https://cqu.cqbys.com/affair/lnjydw/?grade={kwargs["grade"]}&education={kwargs["education"]}&school={kwargs["school"]}&major={value}&University1950Lnjydw%5Bjylb%5D={kwargs["destination"]}&submit=1',
                callback=self.parse_item,
                cb_kwargs=cb_kwargs
            )

    def parse_item(self, response, **kwargs):
        sel = Selector(response)
        list_items = sel.css('ul.infoList')
        for list_item in list_items:
            lnjydw_item = LnjydwItem()
            lnjydw_item['grade'] = kwargs['grade']
            lnjydw_item['education'] = kwargs['education']
            lnjydw_item['school'] = kwargs['school']
            lnjydw_item['major'] = kwargs['major']
            lnjydw_item['destination'] = kwargs['destination']
            lnjydw_item['unit'] = list_item.css('li:nth-child(3)::text').extract_first()
            lnjydw_item['number'] = list_item.css('li:nth-child(4)::text').extract_first()
            yield lnjydw_item
        next_page = sel.css('li.next > a::attr(href)').extract_first()
        url = response.urljoin(next_page)
        yield Request(url=url, callback=self.parse_item, cb_kwargs=kwargs)